<?php

namespace App\Models;

use App\Models\Model;

/**
 * 订单类
 */
class Order extends Model
{
    protected $table = 'shop_order';    

    public function ordergoods()
    {
        return $this->hasOne('App\Models\Ordergoods', 'ORDERGUID', 'GUID');
    }

    public function shopuser()
    {
        return $this->belongsTo('App\Models\User', 'USERGUID', 'GUID');
        // return $this->hasOne('App\Models\User', 'GUID','USERGUID');
    }
    /**
     * 下单时间在3个月之内
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePeriod($query)
    {
        return $query->where('createtime', '>', date('Y-m-d H:i:s', strtotime('-3 months')));
    }

    /**
     * 指定用户所属门店
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeBranch($query)
    {
        $branchguid =  auth()->user()->only(['EMPLOYEEBRANCHGUID']);
        return $query->where('BRANCHGUID', '=', $branchguid['EMPLOYEEBRANCHGUID']);
    }

    /**
     * 指定订单状态
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeState($query, $field, $value)
    {
        return $query->where($field, $value);
    }

    /**
     * 仪表盘数据统计
     */
    public function statistic()
    {
        //用户所在门店，所有订单数量
        $result['order_count'] =  self::period()->branch()->count();
        //用户所在门店,付款订单数量
        $result['order_paid_count'] =  self::period()->branch()->where('ISPAY', 1)->count();
        //用户所在门店,订单总金额
        $result['order_amount'] =  self::period()->branch()->sum('ORDERAMOUNT');
        //用户所在门店,已付款订单总金额
        $result['order_paid_amount'] =  self::period()->branch()->where('ISPAY', 1)->sum('ORDERAMOUNT');
        //已完成订单数
        $result['order_finished_count'] = self::period()->branch()->state('ORDERSTATE', 11)->count();

        return $result;
    }

    /**
     * 门店订单列表数据
     * 订单状态、单个订单总金额,商品信息:商品图、商品标题、商品规格
     */
    public function statList($type = '')
    {
        $per_page = 15;
        $fields = ['GUID', 'ORDERSTATE', 'ORDERAMOUNT', 'PAYTIME', 'USERGUID', 'RECIVERMOBIE'];
        $with = ['ordergoods', 'shopuser'];
        switch ($type) {
            case 'paid': //已付款
                $order_list = self::with($with)
                    ->select($fields)
                    ->period()->branch()->state('ISPAY', 1)
                    ->paginate($per_page);
                break;
            case 'finished':
                $order_list =  self::with($with)
                    ->select($fields)
                    ->period()->branch()->state('ORDERSTATE', 11)
                    ->paginate($per_page);
                break;
            default: //所有订单
                $order_list = self::with($with)
                    ->select($fields)
                    ->period()->branch()
                    ->paginate($per_page);
                break;
        }

        //订单状态
        $order_state = [0 => '未确认', 1 => '已确认', 2 => '未付款', 3 => '已付款', 4 => '未发货', 5 => '已发货', 6 => '未签收', 7 => '已经签收', 8 => '等待退款(申请退款并且商家审核通过后)', 9 => '交易取消', 10 => '交易失败', 11 => '交易完成', 12 => '交易关闭', 13 => '退款申请', 14 => '退款成功', 15 => '未评价', 16 => '已评价', 17 => '订单下载到erp', 18 => '拒绝退款', 20 => '处方订单审核成功', 21 => '处方订单审核失败', 22 => 'OMS待拣货', 23 => 'OMS待下账', 24 => 'OMS已下账', 25 => '退款失败', 26 => '退款中', 27 => '协商退款成功', 28 => '协商退款失败'];


        if ($order_list && count($order_list) > 0) {
            foreach ($order_list as $key => $item) {
                $item['GOODSTITLE'] = @$item->ordergoods->goodsAttach->GOODSTITLE;
                $item['IMG'] = @$item->ordergoods->goodsAttach->HOSTNAME . $item->ordergoods->goodsAttach->URI;
                $item['SPEC'] = @$item->ordergoods->goods->SPEC;
                $item['ORDERSTATE'] = $order_state[$item['ORDERSTATE']];
                $item['MOBILE'] = @isset($item->shopuser) ? $item->shopuser->CODE : '';
                unset($item['ordergoods'], $item['shopuser']);
            }
        }

        return $order_list;
    }
}
